package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.entity.pojo.InspectionRecord;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.vo.pagevo.InspectionRecordPageVo;
import com.hgxd.result.Result;
import com.hgxd.service.InspectionRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 巡检管理 前端控制器
 * </p>
 *
 * @author fsd
 * @since 2023-10-18
 */
@Api(tags = "巡检记录")
@RestController
@RequestMapping("/api/inspectionRecord")
@CrossOrigin
public class InspectionRecordController {
    @Resource(name = "InspectionRecordServiceImpl")
    private InspectionRecordService inspectionRecordService;

    /**
     * 查询最新巡检记录
     * @return
     */
    @GetMapping("getById")
    @ApiOperation(value = "查询最新巡检记录", notes = "无需传参")
    @OperationLogAnnotation(operModule = "巡检记录管理-查询最新巡检记录", operType = "查询", operContent = "查询最新巡检记录")
    public Result<InspectionRecord> getNewInspectionRecord() {
        return Result.ok(inspectionRecordService.list(new QueryWrapper<InspectionRecord>().orderByDesc("id")).get(0));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增巡检记录", notes = "传入巡检记录对象")
    @OperationLogAnnotation(operModule = "巡检记录管理-新增", operType = "新增", operContent = "新增巡检记录")
    public Result<Boolean> insert(@RequestBody InspectionRecord inspectionRecord) {
        return Result.ok(inspectionRecordService.save(inspectionRecord));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改巡检记录", notes = "传入修改的巡检记录对象，需传入id")
    @OperationLogAnnotation(operModule = "巡检记录管理-修改", operType = "修改", operContent = "修改某巡检记录")
    public Result<Boolean> update(@RequestBody InspectionRecord inspectionRecord) {
        return Result.ok(inspectionRecordService.updateById(inspectionRecord));
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除", notes = "传入id")
    @OperationLogAnnotation(operModule = "巡检记录管理-根据id删除", operType = "删除", operContent = "删除某巡检记录")
    public Result<Boolean> delete(@PathVariable("id") Integer id) {
        return Result.ok(inspectionRecordService.removeById(id));
    }

    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    @ApiOperation(value = "批量删除", notes = "根据id批量删除")
    @OperationLogAnnotation(operModule = "巡检记录管理-批量删除", operType = "删除", operContent = "批量删除巡检记录")
    public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
        return Result.ok(inspectionRecordService.removeByIds(ids));
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部", notes = "无需传参")
    @OperationLogAnnotation(operModule = "巡检记录管理-查询", operType = "查询", operContent = "查询所有巡检记录")
    public Result<List<InspectionRecord>> findAll() {
        return Result.ok(inspectionRecordService.list());
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @OperationLogAnnotation(operModule = "巡检记录管理-查询", operType = "查询", operContent = "查询某个巡检记录")
    public Result<InspectionRecord> findOne(@PathVariable("id") Integer id) {
        return Result.ok(inspectionRecordService.getById(id));
    }

    /**
     * 分页模糊查询
     */
    @PostMapping("/get/page")
    @ApiOperation(value = "分页模糊查询", notes = "需要传页码和大小")
    @OperationLogAnnotation(operModule = "巡检记录管理-分页模糊查询", operType = "查询", operContent = "分页查询巡检记录")
    public Result<Page<InspectionRecord>> findPage(
            @RequestBody InspectionRecordPageVo inspectionRecordPageVo) {
        LambdaQueryWrapper<InspectionRecord> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper
                .between(inspectionRecordPageVo.getBeginTime() != null && inspectionRecordPageVo.getEndingTime() != null
                        , InspectionRecord::getDate
                        , inspectionRecordPageVo.getBeginTime()
                        , inspectionRecordPageVo.getEndingTime())
                .eq(!"".equals(inspectionRecordPageVo.getTaskName())
                        , InspectionRecord::getTaskName
                        , inspectionRecordPageVo.getTaskName());
        // 倒序查询
        queryWrapper.orderByDesc(InspectionRecord::getId);
        return Result.ok(inspectionRecordService.page(new Page<>(inspectionRecordPageVo.getPageNum(), inspectionRecordPageVo.getPageSize()), queryWrapper));
    }
}

